﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SuiYuan.UI.Controls.Notify
{
    /// <summary>
    /// LayUINotify.xaml 的交互逻辑
    /// </summary>
    public partial class LayUINotify : Window
    {
        public double TopFrom
        {
            get; set;
        }

        public LayUINotify()
        {
            InitializeComponent();
            this.Loaded += LayUINotify_Loaded;
        }

        private void LayUINotify_Loaded(object sender, RoutedEventArgs e)
        {
            LayUINotify self = sender as LayUINotify;
            if (self != null)
            {
                self.UpdateLayout();
                SystemSounds.Asterisk.Play();//播放提示声
                double right = System.Windows.SystemParameters.WorkArea.Right;//工作区最右边的值
                self.Top = self.TopFrom - self.ActualHeight;
                DoubleAnimation animation = new DoubleAnimation();
                animation.Duration = new Duration(TimeSpan.FromMilliseconds(500));//NotifyTimeSpan是自己定义的一个int型变量，用来设置动画的持续时间
                animation.From = right;
                animation.To = right - self.ActualWidth;//设定通知从右往左弹出
                self.BeginAnimation(Window.LeftProperty, animation);//设定动画应用于窗体的Left属性

                Task.Factory.StartNew(delegate
                {
                    int seconds = 5;//通知持续5s后消失
                    System.Threading.Thread.Sleep(TimeSpan.FromSeconds(seconds));
                    //Invoke到主进程中去执行
                    this.Dispatcher.Invoke(delegate
                    {
                        animation = new DoubleAnimation();
                        animation.Duration = new Duration(TimeSpan.FromMilliseconds(500));
                        animation.Completed += (s, a) => { self.Close(); _dialogs.Remove(self); };//动画执行完毕，关闭当前窗体
                        animation.From = right - self.ActualWidth;
                        animation.To = right;//通知从左往右收回
                        self.BeginAnimation(Window.LeftProperty, animation);
                    });
                });
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            double right = System.Windows.SystemParameters.WorkArea.Right;
            DoubleAnimation animation = new DoubleAnimation();
            animation.Duration = new Duration(TimeSpan.FromMilliseconds(500));
            animation.Completed += (s, a) => { this.Close(); };
            animation.From = right - this.ActualWidth;
            animation.To = right;
            this.BeginAnimation(Window.LeftProperty, animation);
        }

        public static void Show(NotifyType type, string title, string content)
        {
            LayUINotify dialog = new LayUINotify();//new 一个通知
            dialog.Title.Text = title;
            dialog.Content.Text = content;
            switch (type)
            {
                case NotifyType.Info:
                    dialog.Icon.Text = "\ue730";
                    dialog.Border1.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x1E, 0x9F, 0xFF));
                    dialog.Icon.Foreground = new SolidColorBrush(Color.FromArgb(0xFF, 0x1E, 0x9F, 0xFF)); break;
                case NotifyType.Success:
                    dialog.Icon.Text = "\ue869";
                    dialog.Border1.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x96, 0x88));
                    dialog.Icon.Foreground = new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x96, 0x88)); break;
                case NotifyType.Warn:
                    dialog.Icon.Text = "\ue876";
                    dialog.Border1.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xB8, 0x00));
                    dialog.Icon.Foreground = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xB8, 0x00)); break;
                case NotifyType.Error:
                    dialog.Icon.Text = "\ue854";
                    dialog.Border1.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0x57, 0x22));
                    dialog.Icon.Foreground = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0x57, 0x22)); break;
                default:
                    dialog.Icon.Text = "\ue730";
                    dialog.Border1.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x1E, 0x9F, 0xFF));
                    dialog.Icon.Foreground = new SolidColorBrush(Color.FromArgb(0xFF, 0x1E, 0x9F, 0xFF)); break;
            }
            dialog.Time.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            dialog.TopFrom = GetTopFrom();
            _dialogs.Add(dialog);
            dialog.Show();
        }
        public static void ShowInfo(string content)
        {
            Show(NotifyType.Info, "提示", content);
        }
        public static void ShowInfo(string title, string content)
        {
            Show(NotifyType.Info, title, content);
        }

        public static void ShowSuccess(string content)
        {
            Show(NotifyType.Success, "提示", content);
        }
        public static void ShowSuccess(string title, string content)
        {
            Show(NotifyType.Success, title, content);
        }

        public static void ShowWarning(string content)
        {
            Show(NotifyType.Warn, "提示", content);
        }
        public static void ShowWarning(string title, string content)
        {
            Show(NotifyType.Warn, title, content);
        }
        public static void ShowError(string content)
        {
            Show(NotifyType.Error, "提示", content);
        }
        public static void ShowError(string title, string content)
        {
            Show(NotifyType.Error, title, content);
        }

        public static List<LayUINotify> _dialogs = new List<LayUINotify>();
        private static double GetTopFrom()
        {
            //屏幕的高度-底部TaskBar的高度。
            double topFrom = SystemParameters.WorkArea.Bottom - 10;
            bool isContinueFind = _dialogs.Any(o => o.TopFrom == topFrom);

            while (isContinueFind)
            {
                topFrom = topFrom - 103;//此处100是NotifyWindow的高 110-100剩下的10  是通知之间的间距
                isContinueFind = _dialogs.Any(o => o.TopFrom == topFrom);
            }
            if (topFrom <= 0)
                topFrom = System.Windows.SystemParameters.WorkArea.Bottom - 10;
            return topFrom;
        }
    }



    public enum NotifyType
    {
        Info,
        Success,
        Warn,
        Error
    }
}
